/* 
 * File:   GameController.cpp
 * Author: Lex
 *
 * Created on June 20, 2012, 4:14 PM
 */

#include <cstdlib>
#include "Gamecontroller.h"

void Gamecontroller::main(void) {

    std::cout << "Gamecontroller starting\n";

    ControllerState state;

    ihrlndr.setListener(this);

    state = Starting;

    std::cout << "State is: " << state << '\n';
    for (;;) {

        RTOS::event e = wait();

        switch (state) {

            case Starting:
                std::cout << "State is: starting\n";
                starting();
                break;

            case Initializing:
                std::cout << "State is: Initializing\n";
                initializing();
                break;

            case Alive:
                std::cout << "State is: Alive\n";
                alive();
                break;

            case Dead:
                std::cout << "State is: Dead\n";
                dead();
                break;

            case Uploading:
                std::cout << "State is: Uploading\n";
                uploadToPC(PC);
                state = Starting;
                break;

            default:
                std::cout << "State is: default\n";
                // Process for all other cases.
                break;
        }
    }
}

void Gamecontroller::starting() {
    RTOS::event e = wait();

    if (startFlag == e) {
        state = Initializing;
    }
    return;
}

void Gamecontroller::initializing() {
    gameTimer.set(3 S);

    RTOS::event e = wait();

    if (gameTimer == e) {

        initializeGame(120, 8, 8);

        std::cout << "Initialized\n";

        state = Alive;
    }
    return;
}

Gamecontroller::alive() {
    for (;;) {
        ldsHndlr.setLives(playerLives);

        RTOS::event e = wait();

        if (gameTimer == e) {
            std::cout << "GAMETIMER\n";
            spkrHndlr.beep(endGameAudioDuration, endGameAudioFrequency);
            state = Dead;
        }
        if (hitFlag == e) {

            shooter = IRPool.read();
            playerID = shooter.playerID;
            hitBy[playerID]++;

            std::cout << "Hit received. Id is:" << std::dec << playerID << std::cout << '\n';

            if (playerLives >= 1) {
                ldsHndlr.blink(playerHitLedDuration, playerHitLedFrequency);
                spkrHndlr.beep(playerHitAudioDuration, playerHitAudioFrequency);
                playerLives--;
            } else {
                spkrHndlr.beep(playerKillAudioDuration, playerKillAudioFrequency);
                state = Dead;
            }
        }
    }
}

Gamecontroller::dead() {
    RTOS::event e = wait();
    if (logFlag == e) {
        std::cout << "Log me plx?\n";
        state = Uploading;
    }
    return;
}

void Gamecontroller::initializeGame(int gameTime, int players_, int PlayerLives_) {

    playerHitAudioDuration = 2;
    playerHitAudioFrequency = 1000;
    playerHitLedDuration = 5;
    playerHitLedFrequency = 250;
    playerKillAudioDuration = 4;
    playerKillAudioFrequency = 2000;
    endGameAudioDuration = 8;
    endGameAudioFrequency = 500;

    playerLives = PlayerLives_;
    gameTimer.set(gameTime S);
}

void Gamecontroller::startSignalReceived() {
    startFlag.set();
}

void Gamecontroller::hitSignalReceived(struct IRSignal irstruct) {
    IRPool.write(irstruct);
    hitFlag.set();
}

void Gamecontroller::logSignalReceived() {
    logFlag.set();
}

void Gamecontroller::uploadToPC(int style) {
    int x = 0;
    int size = (sizeof (hitBy) * 2) + 6;
    //char message[sizeof(hitBy) / sizeof(int)] = { "Log:" };
    char message[size];
    //message[0] = 'L';
    //message[1] = 'o';
    //message[2] = 'g';
    //message[3] = ':';
    int stringIndex = 4;
    switch (style) {
        case PC:

            for (x = 0; x < sizeof (hitBy); x++) {
                message[stringIndex++] = hitBy[x];
                message[stringIndex++] = ';';
            }
            std::cout << message << "\n";
            break;

        case Visual:
            int x = 0;

            std::cout << "\n====Player scores====\n";
            for (x = 0; x < (sizeof (hitBy) / sizeof (int)); x++) {
                std::cout << "Player " << x << ": " << hitBy[x] << "\n";
            }




    }

}
